package com.ruoyi.sbxz.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.sbxz.domain.ZscqSbxz;
import com.ruoyi.sbxz.domain.ZscqSbxzFj;
import com.ruoyi.sbxz.service.IZscqSbxzService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 申报须知Controller
 *
 * @author huacai
 * @date 2024-10-28
 */
@RestController
@RequestMapping("/sbxz/sbxz")
public class ZscqSbxzController extends BaseController {
    @Resource
    private IZscqSbxzService zscqSbxzService;

    /**
     * 查询申报须知列表
     */
    @PreAuthorize("@ss.hasPermi('sbxz:sbxz:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZscqSbxz zscqSbxz) {
        startPage();
        List<ZscqSbxz> list = zscqSbxzService.selectZscqSbxzList(zscqSbxz);
        return getDataTable(list);
    }

    /**
     * 导出申报须知列表
     */
    @PreAuthorize("@ss.hasPermi('sbxz:sbxz:export')")
    @Log(title = "申报须知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZscqSbxz zscqSbxz) {
        List<ZscqSbxz> list = zscqSbxzService.selectZscqSbxzList(zscqSbxz);
        ExcelUtil<ZscqSbxz> util = new ExcelUtil<ZscqSbxz>(ZscqSbxz.class);
        util.exportExcel(response, list, "申报须知数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ZscqSbxz> util = new ExcelUtil<ZscqSbxz>(ZscqSbxz.class);
        util.importTemplateExcel(response, "申报须知数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "申报须知", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('sbxz:sbxz:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<ZscqSbxz> util = new ExcelUtil<ZscqSbxz>(ZscqSbxz.class);
        InputStream inputStream = file.getInputStream();
        List<ZscqSbxz> list = util.importExcel(inputStream);
        inputStream.close();
        int count = zscqSbxzService.batchInsertZscqSbxz(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取申报须知详细信息
     */
    @PreAuthorize("@ss.hasPermi('sbxz:sbxz:query')")
    @GetMapping(value = "/{sbxzId}")
    public AjaxResult getInfo(@PathVariable("sbxzId") String sbxzId) {
        return success(zscqSbxzService.selectZscqSbxzBySbxzId(sbxzId));
    }

    /**
     * 新增申报须知
     */
    @PreAuthorize("@ss.hasPermi('sbxz:sbxz:add')")
    @Log(title = "申报须知", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZscqSbxz zscqSbxz) {
        zscqSbxz.setUserId(Math.toIntExact(getUserId())); //用户ID
        zscqSbxz.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        zscqSbxz.setCreateBy(getUsername()); //创建人
        return toAjax(zscqSbxzService.insertZscqSbxz(zscqSbxz));
    }

    /**
     * 修改申报须知
     */
    @PreAuthorize("@ss.hasPermi('sbxz:sbxz:edit')")
    @Log(title = "申报须知", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZscqSbxz zscqSbxz) {
        return toAjax(zscqSbxzService.updateZscqSbxz(zscqSbxz));
    }

    /**
     * 删除申报须知
     */
    @PreAuthorize("@ss.hasPermi('sbxz:sbxz:remove')")
    @Log(title = "申报须知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sbxzIds}")
    public AjaxResult remove(@PathVariable String[] sbxzIds) {
        return toAjax(zscqSbxzService.deleteZscqSbxzBySbxzIds(sbxzIds));
    }

    /**
     * 获得申报须知主键ID (UUID)
     */
    @GetMapping("/getSbXzId")
    public AjaxResult getSbXzId() {
        String getSbXzId = IdUtils.fastSimpleUUID();
        return success(getSbXzId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertSbxzFjBySbxzId(@RequestBody ZscqSbxzFj zscqSbxzFj) {
        return toAjax(zscqSbxzService.insertSbxzFjBySbxzId(zscqSbxzFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectSbxzFjList(String sId) {
        ZscqSbxzFj zscqSbxzFj = new ZscqSbxzFj();
        zscqSbxzFj.setSbxzId(sId);
        return AjaxResult.success(zscqSbxzService.selectSbxzFjList(zscqSbxzFj));
    }

    /**
     * 删除申报须知附件
     */
    @Log(title = "申报须知附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteZscqSbxzFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(zscqSbxzService.deleteZscqSbxzFjByFjIds(fjIds));
    }

}
